package com.ideaaedi.springcloud.jd.user.controller.sys;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.ideaaedi.springcloud.jd.commonds.entity.PageInfo;
import com.ideaaedi.springcloud.jd.commonds.entity.Result;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.PhoneUpdateReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysUserAddReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysUserBindDeptPostReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysUserBindRolesReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysUserListReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysUserUpdateReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.SysQueryUserRoleRespVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.SysUserDetailRespVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.SysUserListRespVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.UserDeptPostInfoRespVO;
import com.ideaaedi.springcloud.jd.user.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 系统 - 用户
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 2021.0.1.A
 */
@Validated
@RestController
@ApiSort(value = 10)
@RequestMapping("/sys-user")
@Api(value = "系统-用户", tags = {"系统-用户"})
public class SysUserController {
    
    @Resource
    private SysUserService sysUserService;
    
    /**
     * 新增
     *
     * @param req
     *         参数
     *
     * @return 新增后的用户详情
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增")
    @ApiOperationSupport(order = 1)
    public Result<SysUserDetailRespVO> add(@Validated @RequestBody SysUserAddReqVO req) {
        return Result.success(sysUserService.add(req));
    }
    
    /**
     * 删除
     *
     * @param userId 用户id
     *
     * @return 删除了的用户详情
     */
    @ApiOperation(value = "删除")
    @ApiOperationSupport(order = 2)
    @DeleteMapping("/delete/{userId}")
    public Result<SysUserDetailRespVO> delete(@ApiParam(value = "用户id", required = true)
                                              @NotNull(message = "userId不能为空") @PathVariable("userId") Long userId) {
        return Result.success(sysUserService.delete(userId, false));
    }
    
    /**
     * 更新
     *
     * @param req
     *         参数
     *
     * @return 更新后的用户详情
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新")
    @ApiOperationSupport(order = 3)
    public Result<SysUserDetailRespVO> update(@Validated @RequestBody SysUserUpdateReqVO req) {
        return Result.success(sysUserService.update(req));
    }
    
    /**
     * 列表
     *
     * @param req
     *         参数
     *
     * @return 用户列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "列表")
    @ApiOperationSupport(order = 4)
    public Result<PageInfo<SysUserListRespVO>> list(@Validated @RequestBody SysUserListReqVO req) {
        return Result.success(sysUserService.list(req));
    }
    
    /**
     * 详情
     *
     * @param userId 用户id
     *
     * @return 用户详情
     */
    @ApiOperation(value = "详情")
    @ApiOperationSupport(order = 5)
    @GetMapping("/detail/{userId}")
    public Result<SysUserDetailRespVO> detail(@ApiParam(value = "用户id", required = true)
                                              @NotNull(message = "userId不能为空") @PathVariable("userId") Long userId) {
        return Result.success(sysUserService.detail(userId));
    }
    
    /**
     * 给用户绑定角色
     *
     * @param req
     *         请求模型
     *
     * @return 用户拥有的角色
     */
    @PostMapping("/bind/roles")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "给用户绑定角色")
    public Result<List<SysQueryUserRoleRespVO>> bindRole(@Validated @RequestBody SysUserBindRolesReqVO req) {
        return Result.success(sysUserService.bindRole(req));
    }
    
    /**
     * 查询用户拥有的角色
     *
     * @param userId 用户id
     *
     * @return 用户拥有的角色
     */
    @ApiOperationSupport(order = 7)
    @GetMapping("/query/roles/{userId}")
    @ApiOperation(value = "查询用户拥有的角色")
    public Result<List<SysQueryUserRoleRespVO>> queryRole(@ApiParam(value = "用户id", required = true)
                                                           @NotNull(message = "userId不能为空") @PathVariable("userId") Long userId) {
        return Result.success(sysUserService.queryRole(userId));
    }
    
    /**
     * 给用户绑定部门和职位
     *
     * @param req
     *         请求模型
     *
     * @return 用户绑定的部门和拥有的角色
     */
    @ApiOperationSupport(order = 8)
    @PostMapping("/bind/dept-post")
    @ApiOperation(value = "给用户绑定部门和职位")
    public Result<List<UserDeptPostInfoRespVO>> bindDeptPost(@Validated @RequestBody SysUserBindDeptPostReqVO req) {
        return Result.success(sysUserService.bindDeptPost(req));
    }
    
    /**
     * 查询用户绑定的部门和职位
     *
     * @param userId 用户id
     *
     * @return 用户绑定的部门和拥有的角色
     */
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "查询用户拥有的角色")
    @GetMapping("/query/dept-post/{userId}")
    public Result<List<UserDeptPostInfoRespVO>> queryDeptPost(@ApiParam(value = "用户id", required = true)
                                                              @NotNull(message = "userId不能为空") @PathVariable("userId") Long userId) {
        return Result.success(sysUserService.queryDeptPost(userId));
    }
    
    /**
     * 修改用户手机号
     *
     * @param req 请求
     *
     * @return 用户详情
     */
    @PutMapping("/phone/update")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "修改用户手机号")
    public Result<SysUserDetailRespVO> updatePhone(@Validated @RequestBody PhoneUpdateReqVO req) {
        return Result.success(sysUserService.updatePhone(req));
    }
}
